package ui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import main.ServerMain;
import main.ShareMain;
import objects.ClientImpl;
import exceptions.FileIsNotDirectory;

public class PrincipaleWindow  extends JFrame{

	private static final long serialVersionUID = 1L;
	JPanel panel = new JPanel();
	JPanel panel_Button = new JPanel();
	JButton btnStartServer = new JButton("Start server");
	JButton btnAddClient = new JButton("Add client");
	JButton btnStartClient = new JButton("Open Client Window");
	JTable list_torrent = new JTable();;
	private File file = new File("./torrent");
	private final PrincipaleWindow principaleWindow = this;
	private ServerMain server;
	private boolean serverDemarre;

	public PrincipaleWindow(){
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		final JPanel pbutton = new JPanel();
		pbutton.setLayout(new GridLayout(3,1,5,10));		
		pbutton.add(btnStartServer);
		pbutton.add(btnAddClient);
		pbutton.add(btnStartClient);
		pbutton.add(btnStartServer, BorderLayout.CENTER);
		pbutton.add(btnAddClient, BorderLayout.SOUTH);
		panel_Button.add(pbutton, BorderLayout.CENTER);

		final DefaultTableModel model = new DefaultTableModel();		
		model.addColumn("Client");

		for (final File child : file.listFiles()){
			if (child.isDirectory()){
				model.addRow(new Object[] { child.getName() });
			}
		}

		list_torrent.setModel(model);

		btnStartServer.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				server = new ServerMain();			
				serverDemarre = server.getServerDemarrer();
			}
		});

		btnAddClient.addActionListener(new ActionListener(){
			public void actionPerformed(final ActionEvent e) {
				if (serverDemarre == true){
					new AddClient(principaleWindow);
				}
				else {
					ServerMain.serveurMessage("Server should be launched");
				}
			}
		});


		btnStartClient.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(final ActionEvent arg0) {
				if (serverDemarre == true){
					new ShareMain();

					final Object cellule = list_torrent.getModel().getValueAt(list_torrent.getSelectedRow(), list_torrent.getSelectedColumn());
					File fichier = null;
					for (final File child : file.listFiles()){
						if (child.getName().equals(cellule)){
							fichier = child;
						}
					}

					try {
						new ClientWindow(new ClientImpl(fichier, "rmi://localhost:1099/Tracker" ));
					} catch (final RemoteException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (final MalformedURLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (final FileIsNotDirectory e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (final NotBoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				else {
					ServerMain.serveurMessage("Lancer le serveur avant");
				}

			}

		});


		final JScrollPane scroll = new JScrollPane(list_torrent);
		panel.add(scroll);
		this.getContentPane().add("West", panel);
		this.getContentPane().add("East", panel_Button);


		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	public Object getClient(){
		final int row = list_torrent.getSelectedRow();
		final int col = list_torrent.getSelectedColumn();
		return list_torrent.getModel().getValueAt(row, col);
	}


	public JTable getList_torrent() {
		return list_torrent;
	}

	public File getFile() {
		return file;
	}

	public void setFile(final File file) {
		this.file = file;
	}

	public void setTableau(final JTable tableau) {
		this.list_torrent = tableau;
	}	
}
